Yacon.widget.TInput = function(element){
	this.element = element;
};

Yacon.widget.TInput.prototype = {
	
	/* 获取HTML信息 */	
	getHtml: function(plusHtml){	
		
		var html = "";
		
		if(!Yacon.isTrue(this.element.lableVisible) 
		&& !Yacon.isTrue(this.element.hintVisible)
		&& !Yacon.isTrue(this.element.isCheck)){
			html += "<div>";
		}else{
			html += "<div class='input-group'>";
		}
		
		var lableClass = "";
		
		if(this.isEnable() || this.parent.view.isStatic){
			lableClass = "bk-bg-primary";
		}else{
			lableClass = "bk-bg-brown";
		}
		
		if(this.element.btnState){
			lableClass = "bk-bg-"+this.element.btnState;
		}
		
		if(Yacon.isTrue(this.element.lableVisible)){
		
			html += "<span class='input-group-btn "+lableClass+"' "+this.getColorStyle()+" >";
			
			html += new Yacon.widget.TButton(this.element.labelMenu).getFormHtml();
			
			html += "</span>";		
		}
		
		html += "<input ";
		
		html += " id='"+this.element.name+"' ";
		html += " class='form-control lily-field' name='"+this.element.name+"' type='text' ";
		
		html += " field-name='"+this.element.name+"' ";
		
		html += " field-type='input' ";
		
		html += " field-is-null='"+this.element.isNull+"' "
		html += " field-max-length='"+this.element.maxLength+"' "
		
		var labelText = this.element.text;
		if(labelText.endWith(" *")){
			labelText = labelText.substring(0,labelText.length-2);
		}else if(labelText.endWith("*")){
			labelText = labelText.substring(0,labelText.length-1);
		}
		html += " field-label='"+labelText+"' ";
		
		if(this.element.blank){
			html += " placeholder='"+this.element.blank+"' ";
		}
		
		if(!this.isEnable()){
			html += " disabled ";
		}
		
		html += this.getDefaultValue();
		
		html += " />";
		
		if(Yacon.isTrue(this.element.hintVisible)){
			
			for(var i=0;i < this.element.hintMenu.length;i++){
				
				var curButton = this.element.hintMenu[i];
				
				html += "<span class='input-group-btn "+lableClass+"' "+this.getButtonColorStyle(curButton)+" >";
				
				html += new Yacon.widget.TButton(curButton).getFormHtml();
				
				html += "</span>";
				
			}
			
		}
		
		if(plusHtml){
			html += plusHtml;
		}
		
		html += "</div>";
		
		return html;
	},
	
	getLableHtml: function(){
		
		var html = "";
		var lable = this.element.text;
		var lableIco = this.element.ico;
		var lableType = this.element.viewType;
		
		if(Yacon.isEmpty(lableIco)){
			lableIco = "glyphicon glyphicon-tag";
		}
		
		if(lableType == "all"){
			html += "<span class='"+lableIco+"'></span>&nbsp;&nbsp;"+lable;
		}
		else if(lableType == "ico"){
			html += "<span class='"+lableIco+"'></span>";
		}
		else if(lableType == "label"){
			html += lable;
		}
		
		return html;
	},
	
	isEnable: function(){
		
		this.page = this.parent.page;
		var state = this.page.element.parameter.state;
		
		if(Yacon.isEmpty(state)){
			state = "active";
		}
		
		if(!this.element.isEnable){
			return false;
		}
		
		if(state == "insert"){
			return this.element.isInsert;
		}else if(state == "edit"){
			return this.element.isEdit;
		}else if(state == "past"){
			return false;
		}
		else{
			return true;
		}
		
	},
	
	getDefaultValue: function(){
		
		this.page = this.parent.page;
		var state = this.page.element.parameter.state;
		
		if(Yacon.isEmpty(state)){
			state = "active";
		}
		
		if(!Yacon.isEmpty(this.element.commonVal)){			
			return " value='"+this.element.commonVal+"' ";		
		}
		
		else{
			
			if(state == "insert"){
				if(!Yacon.isEmpty(this.element.insertVal)){			
					return " value='"+this.element.insertVal+"' ";		
				}else{
					return "";
				}
			}
			
			else if(state == "edit"){
				if(!Yacon.isEmpty(this.element.editVal)){			
					return " value='"+this.element.editVal+"' ";	
				}else{
					return "";
				}
			}
			
			else {
				return "";
			}
			
		}
	},
	
	addEvent: function(){
		
		var success = "<span class='glyphicon glyphicon-ok form-control-feedback'></span>";
		var error = "<span class='glyphicon glyphicon-remove form-control-feedback'></span>";
		var warning = "<span class='glyphicon glyphicon-warning-sign form-control-feedback'></span>";
		var self = this;
		
		if(Yacon.isTrue(this.element.isCheck)){
			
			$(".form-control.lily-field[field-name='"+this.element.name+"']").unbind("blur");
			$(".form-control.lily-field[field-name='"+this.element.name+"']").blur(function(){
				
				var parDiv = $(this).parent();
				if(!parDiv.hasClass("has-feedback")){
					parDiv.addClass("has-feedback");
				}
				
				var activeVal = $(this).val();
				
				if(!Yacon.isTrue(self.element.isNull)){
					
					if(activeVal.trim() == ""){				
						parDiv.addClass("has-error");
						$(this).after(error);				
						return;
					}
				}
				
			});
			
			$(".form-control.lily-field[field-name='"+this.element.name+"']").unbind("focus");
			$(".form-control.lily-field[field-name='"+this.element.name+"']").focus(function(){
				
				var parDiv = $(this).parent();
				
				parDiv.removeClass("has-feedback");
				parDiv.removeClass("has-success");
				parDiv.removeClass("has-error");
				parDiv.removeClass("has-warning");
				
				$(this).parent().find(".glyphicon.glyphicon-ok.form-control-feedback").remove();
				$(this).parent().find(".glyphicon.glyphicon-remove.form-control-feedback").remove();
				$(this).parent().find(".glyphicon.glyphicon-warning-sign.form-control-feedback").remove();
				
				
			});
			
		}
		
	},
	
	getColorStyle: function(){
		
		var html = "";
		
		var btnColor = "";
		
		if(!Yacon.isEmpty(this.element.fontColor)){
			btnColor += "color:"+this.element.fontColor+" !important;";
		}
		
		if(!Yacon.isEmpty(this.element.backColor)){
			btnColor += " background-color:"+this.element.backColor+" !important;";
			btnColor += " border-color:"+this.element.backColor+" !important;";
		}
		
		if(!Yacon.isEmpty(btnColor)){
			html += " style='"+btnColor+"' ";
		}
		
		return html;
		
	},
	
	getButtonColorStyle: function(button){
		
		var html = "";
		
		var btnColor = "";
		
		if(!Yacon.isEmpty(button.fontColor)){
			btnColor += "color:"+button.fontColor+" !important;";
		}
		
		if(!Yacon.isEmpty(button.backColor)){
			btnColor += " background-color:"+button.backColor+" !important;";			
		}
		
		btnColor += " border-right:2px solid #FFFFFF !important; ";
		
		if(!Yacon.isEmpty(btnColor)){
			html += " style='"+btnColor+"' ";
		}
		
		return html;
		
	},
	
	getInnerHtml: function(){
		
		var html = "";
		
		var lableClass = "";
		
		if(this.isEnable() || this.parent.view.isStatic){
			lableClass = "bk-bg-primary";
		}else{
			lableClass = "bk-bg-brown";
		}
		
		if(this.element.btnState){
			lableClass = "bk-bg-"+this.element.btnState;
		}
		
		if(Yacon.isTrue(this.element.lableVisible)){
		
			html += "<span class='input-group-btn "+lableClass+"' "+this.getColorStyle()+" >";
			
			html += new Yacon.widget.TButton(this.element.labelMenu).getFormHtml();
			
			html += "</span>";		
		}
		
		html += "<input ";
		
		html += " id='"+this.element.name+"' ";
		html += " class='form-control lily-field' name='"+this.element.name+"' type='text' ";
		
		html += " field-name='"+this.element.name+"' ";
		
		html += " field-type='input' ";
		
		if(this.element.blank){
			html += " placeholder='"+this.element.blank+"' ";
		}
		
		if(!this.isEnable()){
			html += " disabled ";
		}
		
		html += this.getDefaultValue();
		
		html += " />";
		
		if(Yacon.isTrue(this.element.hintVisible)){
			
			for(var i=0;i < this.element.hintMenu.length;i++){
				
				var curButton = this.element.hintMenu[i];
				
				html += "<span class='input-group-btn "+lableClass+"' "+this.getButtonColorStyle(curButton)+" >";
				
				html += new Yacon.widget.TButton(curButton).getFormHtml();
				
				html += "</span>";
				
			}
			
		}
		
		return html;
		
	}

};